package com.wuhanins.insmytestdemo.app.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.wuhanins.common.utils.INSLog;
import com.wuhanins.insmytestdemo.base.BaseActivity;
import com.wuhanins.insmytestdemo.databinding.ActivityLocationBaiduBinding;
import com.wuhanins.insmytestdemo.domainService.provider.utils.MyLocation;

import java.io.IOException;
import java.util.List;

/**
 * @author: Gab
 * @Project: fileService
 * @Pcakage: com.wuhanins.insmytestdemo.ui.test.LocationActivity
 * @Date: 2024年01月30日 10:19
 * @Description:
 */
public class LocationBaiDuTestActivity extends BaseActivity<ActivityLocationBaiduBinding> {

    private MyLocation myLocation;

    public static void start(Context context) {
        Intent starter = new Intent(context, LocationBaiDuTestActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void iView() {

    }

    @Override
    protected void iData() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(ActivityLocationBaiduBinding.class);
//        requirePermission();
        LocationListener locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                setLocation(location);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {
                setLocation(null);
                Log.e("TAG", "onProviderDisabled: " + provider);
            }

            @Override
            public void onProviderDisabled(String provider) {
                Log.e("TAG", "onProviderEnabled: " + provider);
            }
        };
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 8, locationListener);
    }

    private void requirePermission() {
        MyLocation.LocationResult locationResult = new MyLocation.LocationResult() {
            @Override
            public void gotLocation(Location location) {
                setLocation(location);
            }
        };
        myLocation = new MyLocation();
        myLocation.getLocation(LocationBaiDuTestActivity.this, locationResult);
    }


    private void setLocation(Location location) {
        if (location != null) {
            String mfeatureName;
            Geocoder geocoder = new Geocoder(LocationBaiDuTestActivity.this);
            List<Address> addList = null;// 解析经纬度
            try {
                addList = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (addList != null && addList.size() > 0) {
                for (int i = 0; i < addList.size(); i++) {
                    Address add = addList.get(i);
                    mfeatureName = add.getFeatureName();
                    INSLog.e("mfeatureName", mfeatureName);
//                    binding.tvLocation.setText(mfeatureName);
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myLocation.cancelTimer();
    }
}
